#pragma once
#include <windows.h>

#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
	WSAOVERLAPPED overlapped;
	DWORD threadId;
} WSAOverlappedWrapper;

class ScClient
{
public:
	ScClient();
	~ScClient();

	int startConnection(const char *serverName, const char *serverPort);
	int send(const char *buf, int len, DWORD targetIp);
	int recv(LPWSABUF lpBuffers, DWORD dwBufferCount, LPDWORD lpNumberOfBytesRecvd, DWORD threadId, DWORD &fromIp);
	void release();
	void clearRecvOverLappedThreadId();

private:
	BOOL assignDataBuf(const char *buf, int len, WSABUF **pDataBuf, int &bufCnt, DWORD targetIp);
	void releaseDataBuf(WSABUF **pDataBuf, int bufCnt);
	WSAOVERLAPPED * getRecvOverLapped(DWORD threadId);

private:
	SOCKET connSocket;
	struct addrinfo *addrInfo;
	WSAOVERLAPPED sendOverlapped;
	WSAOverlappedWrapper recvOverlapped[2];
	CRITICAL_SECTION criticalSection;
	char serverName[64];
	char serverPort[8];
};

extern ScClient *gScClientCoor;
extern ScClient *gScClientGame;

#define DLL_EXPORT __declspec(dllexport)

#ifdef __cplusplus
extern "C" {
#endif

extern void DLL_EXPORT setScServerConfig(LPVOID params);

#ifdef __cplusplus
}
#endif